<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests;
use App\Repository\Models\PermissionRole;
use App\Repository\Repositories\Interfaces\ActionRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Str;


class ActionController extends BaseController
{
    private $actionRepository;
    private $requ_data;

    public function __construct(ActionRepository $actionRepository)
    {
        parent::__construct();
        $this->actionRepository = $actionRepository;
    }

    /**
     *所有操作的路由操作
     */
    public function all()
    {
        $router = app('router');
        $routes = $router->getRoutes();
        $route_names = [];
        foreach ($routes as $route) {
            $name = array_get($route,'action.as');
            if(Str::startsWith($name,'backend')){
                $route_names[] = $name;
            }
        }
        $route_names = array_unique($route_names);
        return $this->success('ok',['list' => $route_names]);
    }
    
    /**
     * 操作列表
     *
     * @return \Illuminate\Http\Response1
     */
    public function index()
    {
        $ret = $this->actionRepository->orderBy('id','desc')->paginate($this->perPage);
        $data = ['list' => $ret['data'],'page' => $ret['meta']['pagination']];
        return $this->success('ok',$data);
    }

    /**
     * 增加操作
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'group'       => 'required',
            'name'        => 'required',
            'action'      => 'required|unique:actions,action',
            'description' => 'required',
        ],[
            'group.required'       => '操作名称不能为空',
            'name.required'        => '操作分组不能为空',
            'action.required'      => '操作描述不能为空',
            'description.required' => '操作标识不能为空',
            'action.unique'        => '操作标识必须唯一',
        ]);
        try {
            if ($action = $this->actionRepository->create($request->all())) {
                return $this->success('创建操作成功',$action['data']);
            }
        }
        catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     *  操作详情
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $ret = $this->actionRepository->find($id);
        return $this->success('ok', $ret['data']);
    }


    /**
     * 修改操作
     * @param  int                                    $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        $this->validate($request,[
            'group'       => 'required',
            'name'        => 'required',
            'action'      => 'required|unique:actions,action,' . $id,
            'description' => 'required',
        ],[
            'group.required'       => '操作名称不能为空',
            'name.required'        => '操作分组不能为空',
            'action.required'      => '操作描述不能为空',
            'description.required' => '操作标识不能为空',
            'action.unique'        => '操作标识必须唯一',
        ]);
        try {
            if ($action = $this->actionRepository->update($request->all(),$id)) {
                return $this->success("编辑操作成功",$action['data']);
            }
        }
        catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 删除操作
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            if ($this->actionRepository->delete($id)) {
                return $this->success("删除操作成功");
            }
        }
        catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 操作权限
     */
    public function action_per(Request $request){

        $data=$request->all();

        $this->requ_data=$data['action'];
        $ret = $this->actionRepository->with(['actionPermission'])->scopeQuery(function($query){
            return $query->whereIn('id',$this->requ_data);
        })->orderBy('id','desc')->all();
        $cc=[];
        foreach($ret['data'] as $k =>$v){
            if(!empty($v['action_permission'])){
                $roledata=[
                    'permission_id'=>$v['action_permission']['permission_id'],
                    'role_id'=>$data['role_id']
                ];
                $cc[]=PermissionRole::query()->create($roledata);
            }
        }

        return $this->success('ok',$ret['data']);
    }
}
